package wangjg.android.test;

import wangjg.android.R;
import wangjg.android.common.jni.JNITest;
import wangjg.android.service.FloatingService;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ServiceTestActivity extends Activity {
	private Button startService, stopService, bindService, unbindService;
	private Button btn_test;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.service_test);
		startService = (Button) findViewById(R.id.startButton);
		stopService = (Button) findViewById(R.id.stopButton);
		bindService = (Button) findViewById(R.id.bindButton);
		btn_test = (Button) findViewById(R.id.btn_test);

		unbindService = (Button) findViewById(R.id.unbindButton);
		startService.setOnClickListener(startListener);
		stopService.setOnClickListener(stopListener);
		bindService.setOnClickListener(bindListener);
		unbindService.setOnClickListener(unbindListener);
		btn_test.setOnClickListener(testListener);
	}

	private OnClickListener startListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction("wangjg.android.service.FloatingService");
			startService(intent);
		}

	};
	
	private OnClickListener stopListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction("wangjg.android.service.FloatingService");
			stopService(intent);
		}

	};
	
	private OnClickListener bindListener = new OnClickListener() {
	
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction("wangjg.android.service.FloatingService");
			bindService(intent, conn, Service.BIND_AUTO_CREATE);
		}
	
	};
	
	private OnClickListener unbindListener = new OnClickListener() {
	
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction("wangjg.android.service.FloatingService");
			unbindService(conn);
		}
	
	};
	
	private OnClickListener testListener = new OnClickListener() {
	
		@Override
		public void onClick(View v) {
			String s = new JNITest().hello();
			Log.i("jni", s);
			Toast.makeText(ServiceTestActivity.this, "JNI:" + s,
					Toast.LENGTH_LONG).show();
		}
	
	};
	
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			Log.i("SERVICE", "connection success");
			Toast.makeText(ServiceTestActivity.this, "connection success", Toast.LENGTH_LONG).show();
			View view = LayoutInflater.from(ServiceTestActivity.this).inflate(R.layout.buddy_list_item, null);
			FloatingService.MyBinder mb=(FloatingService.MyBinder)binder;
			mb.getService().setView(view);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.i("SERVICE", "connection success");
			Toast.makeText(ServiceTestActivity.this, "connection failure", Toast.LENGTH_LONG).show();
		}

	};

}